using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.ComponentModel;

namespace produccion.clases
{

    class CLSMAECIUP : ObservableObject
    {

        MDOgeneral mod = new MDOgeneral();

        #region Metodos basicos tabla
            public List<CLSMAECIUP> listaCiudad()
            {
                try
                { 
                       string query = "select CODCIU_CIU,NOMCIU_CIU,DEPCIU_CIU,VLRKIL_CIU,FACCIU_CIU,DTOCIU_CIU,INDCIU_CIU from MAECIUP Order by NOMCIU_CIU,DEPCIU_CIU";
                        DataTable val = mod.consulta(query);
                        List<CLSMAECIUP> lisCiudad = new List<CLSMAECIUP>();
                        foreach (DataRow r in val.Rows)
                        {
                            lisCiudad.Add(new CLSMAECIUP { CODCIU_CIU = r["CODCIU_CIU"].ToString(), NOMCIU_CIU = r["NOMCIU_CIU"].ToString(), DEPCIU_CIU = r["DEPCIU_CIU"].ToString(), VLRKIL_CIU = Convert.ToDouble(r["VLRKIL_CIU"]), FACCIU_CIU = r["FACCIU_CIU"].ToString().Replace(",", "."), DTOCIU_CIU = r["DTOCIU_CIU"].ToString().Replace(",", "."), INDCIU_CIU = r["INDCIU_CIU"].ToString() });
                        }
                        return lisCiudad;
                }
                catch (Exception e) { throw e; }
            }
            public int actualizar()
            {
                try
                {
                    string query = "update MAECIUP set NOMCIU_CIU='" + NOMCIU_CIU + "',DEPCIU_CIU='" + DEPCIU_CIU + "',VLRKIL_CIU=" + VLRKIL_CIU + ",FACCIU_CIU=" + FACCIU_CIU + ",DTOCIU_CIU=" + DTOCIU_CIU + ",INDCIU_CIU='" + INDCIU_CIU + "' where (CODCIU_CIU='" + CODCIU_CIU + "')";
                    return mod.comando(query);
                }
                catch (Exception e) { throw e; }
            }
            public int insertar()
            {
                try
                {
                    string query = "insert into MAECIUP (CODCIU_CIU,NOMCIU_CIU,DEPCIU_CIU,VLRKIL_CIU,FACCIU_CIU,DTOCIU_CIU,INDCIU_CIU) values ('" + CODCIU_CIU + "','" + NOMCIU_CIU + "','" + DEPCIU_CIU + "'," + VLRKIL_CIU + "," + FACCIU_CIU + "," + DTOCIU_CIU + ",'" + INDCIU_CIU + "')";
                    return mod.comando(query);
                }
                catch (Exception e) { throw e; }
            }
            public void consultar() 
            {
                try
                {
                    string query = "select CODCIU_CIU,NOMCIU_CIU,DEPCIU_CIU,VLRKIL_CIU,FACCIU_CIU,DTOCIU_CIU,INDCIU_CIU from MAECIUP where CODCIU_CIU='" + CODCIU_CIU + "'";
                    DataTable val = mod.consulta(query);
                    if (val.Rows.Count > 0)
                    {
                        this.NOMCIU_CIU = val.Rows[0]["NOMCIU_CIU"].ToString();
                        this.DEPCIU_CIU = val.Rows[0]["DEPCIU_CIU"].ToString();
                        this.VLRKIL_CIU = Convert.ToDouble(val.Rows[0]["VLRKIL_CIU"]);
                        this.FACCIU_CIU = val.Rows[0]["FACCIU_CIU"].ToString().Replace(",", ".");
                        this.DTOCIU_CIU = val.Rows[0]["DTOCIU_CIU"].ToString().Replace(",", ".");
                        this.INDCIU_CIU = val.Rows[0]["INDCIU_CIU"].ToString();
                        EXISTE = true;
                        CAMBIO = false;
                    }
                    else
                    {
                         EXISTE = false;
                    }
            
                }
                catch (Exception e) { throw e; }
            }
            public int eliminar()
            {
                try
                {
                    if (!exiMov())
                    {
                        string query = "delete from MAECIUP where (CODCIU_CIU='" + CODCIU_CIU + "')";
                        return mod.comando(query);
                    }
                    return 0;
           
                }
                catch (Exception e) { throw e; }
            }
            public List<CLSMAECIUP> listarBaseCiu()
            {
                try
                {
                    string query = "select CODCIU_CIU, NOMCIU_CIU from MAECIUP order by NOMCIU_CIU";
                    DataTable val = mod.consulta(query);
                    List<CLSMAECIUP> lisCiudad = new List<CLSMAECIUP>();
                    foreach (DataRow r in val.Rows)
                    {
                        lisCiudad.Add(new CLSMAECIUP { CODCIU_CIU = r["CODCIU_CIU"].ToString(), NOMCIU_CIU = r["NOMCIU_CIU"].ToString() });
                    }
                    return lisCiudad;
                }
                catch (Exception e) { throw e; }
            }
            public Boolean exiMov() 
            {
                try 
                {            
                    //Configuracion empresa
                    string query = "select count(*) from CFGEMPP where CODCIU_CFG='" + CODCIU_CIU + "'";
                    if (!mod.consulta(query).Rows[0][0].ToString().Equals("0")) 
                    {
                        return true;
                    }
                    //Maestro de proveedores
                    query = "select count(*) from MAEPRVP where CODCIU_PRV='"+CODCIU_CIU+"'";
                    if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
                    {
                        return true;
                    }
                    //Maestro de clientes
                    query = "select count(*) from MAECLIP where CODCIU_CLI='" + CODCIU_CIU + "'";
                    if (!mod.consulta(query).Rows[0][0].ToString().Equals("0"))
                    {
                        return true;
                    }
                    return false;
                } catch(Exception ex) { throw(ex);}
            }
            public void conCodCiu() 
            {
                try 
                {
                    int aux = 1;
                    string query = "select max(codciu_ciu) from maeciup where substr(codciu_ciu,0,1) in ('0','1','2','3','4','5','6','7','8','9')";
                    DataTable val =mod.consulta(query);
                    if (val.Rows.Count > 0) {
                        aux = Convert.ToInt32(val.Rows[0][0]);
                        aux = aux + 1;
                    }
                    string valor = aux.ToString();
                    while (valor.Length < 4) 
                    {
                        valor = "0" + valor;
                    }
                    CODCIU_CIU = valor;
                }
                catch (Exception ex) { throw ex; }
            }

        #endregion

        #region otros
            public Boolean existe() {
               try{
              string query="select  coalesce(count(CODCIU_CIU),0) from MAECIUP where (CODCIU_CIU='"+CODCIU_CIU+"')";
              DataTable val = mod.consulta(query);
              if (val.Rows[0][0].ToString().Equals("0")){
              return false;
              }
              return true;
              }catch (Exception e) { throw e; }
            }

            public DataTable listar()
            {
                try{
                string query = "select CODCIU_CIU,NOMCIU_CIU from MAECIUP";
                return mod.consulta(query);
                }catch (Exception e) { throw e; }
            }

            public DataTable buscar() {
               try{
              string query="select CODCIU_CIU as Codigo,NOMCIU_CIU as Nombre,DEPCIU_CIU as Departamento from  MAECIUP where ";


              if(NOMCIU_CIU!=""){
                  query=query+"UPPER(NOMCIU_CIU) like '%"+NOMCIU_CIU.ToUpper()+"%' and ";
              }

              query = query.Remove(query.Length - 4).Trim() + " Order by NOMCIU_CIU asc";
      
                  return mod.consulta(query);
               }catch (Exception e) { throw e; }
            }

        #endregion

        #region  Propiedades tabla MAECIUP
            private string _CODCIU_CIU;
            public string CODCIU_CIU
            {
                get { return _CODCIU_CIU;}
                set { _CODCIU_CIU=value; RaisePropertyChangedEvent("CODCIU_CIU"); }
            }
            private string _NOMCIU_CIU;
            public string NOMCIU_CIU
            {
                get { return _NOMCIU_CIU;}
                set 
                {
                    if (_NOMCIU_CIU != value) { CAMBIO = true; } 
                    _NOMCIU_CIU=value;
                     RaisePropertyChangedEvent("NOMCIU_CIU");
                }
            }
            private string _DEPCIU_CIU;
            public string DEPCIU_CIU
            {
                get { return _DEPCIU_CIU;}
                set 
                {
                    if (_DEPCIU_CIU != value) { CAMBIO = true; } 
                    _DEPCIU_CIU=value;
                    RaisePropertyChangedEvent("DEPCIU_CIU");
                }
            }
            private double _VLRKIL_CIU;
            public double VLRKIL_CIU
            {
                get { return _VLRKIL_CIU;}
                set 
                {
                    if (_VLRKIL_CIU!=value) { CAMBIO = true; } 
                    _VLRKIL_CIU=value;
                    RaisePropertyChangedEvent("VLRKIL_CIU");
                }
            }
            private string _FACCIU_CIU;
            public string FACCIU_CIU
            {
                get { return _FACCIU_CIU;}
                set 
                {
                    if (_FACCIU_CIU != value) { CAMBIO = true; } 
                    _FACCIU_CIU=value;
                    RaisePropertyChangedEvent("FACCIU_CIU");
                }
            }
            private string _DTOCIU_CIU;
            public string DTOCIU_CIU
            {
                get { return _DTOCIU_CIU;}
                set
                {    
                    if (_DTOCIU_CIU != value) { CAMBIO = true; } 
                    _DTOCIU_CIU = value;
                    RaisePropertyChangedEvent("DTOCIU_CIU");
                }
            }
            private string _INDCIU_CIU;
            public string INDCIU_CIU
            {
                get { return _INDCIU_CIU;}
                set 
                {
                    if (_INDCIU_CIU != value) { CAMBIO = true; }
                    _INDCIU_CIU=value;
                    RaisePropertyChangedEvent("INDCIU_CIU");
                }
            }
            private Boolean _CAMBIO=false;
            public Boolean CAMBIO
            {
                get { return _CAMBIO; }
                set { _CAMBIO = value; RaisePropertyChangedEvent("CAMBIO"); }
            }
            private Boolean _EXISTE=false;
            public Boolean EXISTE
            {
                get { return _EXISTE; }
                set { _EXISTE = value; RaisePropertyChangedEvent("EXISTE"); }
            }

        #endregion

    }

}
